#!/usr/bin/perl -w
use Modern::Perl;

my $a = $ARGV[0] or die "Se esperaba una cadena";

sub analyze {
  $_[0] =~ s{([-+*/]|\d+)}{ $1 }g;
  split /\s+/, $_[0];
}

sub calculate {
  my @b;
  foreach (@_) {
    if (/\d+/) {
      unshift @b, $_;
    } elsif (m{[+-/*]}) {
      my $c = shift @b or die("Faltan args!");
      my $d = shift @b or die("Faltan args!");
      unshift @b, eval($c.' '.$_.' '.$d);
    }
  }
  return $b[0]."\n";
}

print calculate(analyze($a));
